package com.example.myapplication.ui.modify;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.databinding.ModifyFragmentBinding;
import com.example.myapplication.pojo.SingleUser;
import com.example.myapplication.utils.MySqliteHelper;


public class ModifyFragment extends Fragment {

    private ModifyViewModel mViewModel;
    private ModifyFragmentBinding binding;
    private String TAG="ModifyFragment ";

    public static ModifyFragment newInstance() {
        return new ModifyFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        mViewModel = new ViewModelProvider(this).get(ModifyViewModel.class);
        binding = DataBindingUtil.inflate(inflater, R.layout.modify_fragment, container, false);
        binding.setData(mViewModel);

        binding.sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password=binding.password.getText().toString();
                String two_password=binding.twoPassword.getText().toString();
                if(!password.equals(two_password)){
                    Toast.makeText(getActivity(), "对不起两次密码不同", Toast.LENGTH_SHORT).show();
                }else {
                    MySqliteHelper mySqliteHelper=new MySqliteHelper(getActivity());
                    SQLiteDatabase db =mySqliteHelper.getReadableDatabase();
                    ContentValues cValue = new ContentValues();
                    //添加用户名
                    //添加密码
                    cValue.put("password",password);
                    Log.d(TAG, password);
                    //调用insert()方法插入数据
                   int i= db.update("user",cValue,"name =?",new String[]{SingleUser.getInstance().getUser().getName()});
                   if(i>0){
                       Toast.makeText(getActivity(), "修改成功", Toast.LENGTH_SHORT).show();
                       SingleUser.getInstance().getUser().setPassword(password);
                   }else {
                       Toast.makeText(getActivity(), "修改失败", Toast.LENGTH_SHORT).show();
                   }
                   db.close();
                    getActivity().finish();
                }
            }
        });

        /**
         * 结束当前activity
         */
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        mViewModel = ViewModelProviders.of(this).get(ModifyViewModel.class);
        // TODO: Use the ViewModel
    }

}